package com.woniu.microchargeserver.chargemag.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.woniu.microchargeserver.chargemag.service.IChargedItemsService;
import com.woniu.microchargeserver.common.util.TraceUtil;
import com.woniu.microcommonsserver.entity.ChargedItemsInfoEntity;
import com.woniu.microcommonsserver.param.ResponseMsg;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**收费项目的controller
 * @author: hejie
 * @date: 2021/1/23 7:35
 * @description:
 */
@RestController
@RequestMapping("/chargedItems")
public class ChargedItemsController {

    @Resource
    private IChargedItemsService chargedItemsServiceImpl;

    /**
     * 修改 一个收费项目
     * @author hejie
     * @param chargedItemsInfo
     * @return
     */
    @PutMapping(value="/update",produces = {"application/json;charset=utf-8"})
    public ResponseMsg updateAdmin(@RequestBody ChargedItemsInfoEntity chargedItemsInfo){
        System.out.println("\n"+"\tuserInfo="+chargedItemsInfo+"\t"+ TraceUtil.positionTrace());
        ResponseMsg responseMsg = new ResponseMsg(200,"修改成功",null);
        chargedItemsServiceImpl.updateChargedItems(chargedItemsInfo);
        return responseMsg;
    }

    /**新增一个 收费项目
     * @author hejie
     * @param chargedItemsInfo
     * @return
     */
    @PostMapping(value="/more",produces = {"application/json;charset=utf-8"})
    public ResponseMsg saveAdmin(ChargedItemsInfoEntity chargedItemsInfo){
        System.out.println("\nchargedItemsInfo="+chargedItemsInfo+"\t"+ TraceUtil.positionTrace());

        ResponseMsg responseMsg = new ResponseMsg(200,"新增成功",null);
        chargedItemsServiceImpl.saveChargedItems(chargedItemsInfo);
        return responseMsg;
    }

    /**
     * 删除 一个收费项目
     * @author hejie
     * @param id
     * @return
     */
    @DeleteMapping(value="/{id}", produces = {"application/json;charset=utf-8"})
    public ResponseMsg delete(@PathVariable("id") Long id){
        ResponseMsg responseMsg = new ResponseMsg(200,"删除成功",null);

        chargedItemsServiceImpl.deleteChargedItems(id);
        return responseMsg;
    }

    /**
     * 分页查询数据
     * @author hejie
     * @param pageNum 第几页
     * @param pageSize 每页显示多少条数据
     * @param column 排序的字段
     * @param order 升序还是降序
     * @param chargedItemsInfo 查询数据的部分条件
     * @param chargeCodeOrName 模糊：售给编码或收费明名称
     * @return
     */
    @RequestMapping(value="/page",produces = {"application/json;charset=utf-8"})
    public String findAllByParams(@RequestParam(required = false,defaultValue = "1") int pageNum,
                                                      @RequestParam(required = false,defaultValue = "10")int pageSize,
                                                      String column, String order, ChargedItemsInfoEntity chargedItemsInfo, String chargeCodeOrName){
        System.out.println("\nchargedItemsInfo="+chargedItemsInfo+"\tpageNum="+pageNum+"\tpageSize="+pageSize+"\tchargeCodeOrName="+chargeCodeOrName+"\tcolumn="+column+"\torder="+order+ TraceUtil.positionTrace());

        PageInfo<ChargedItemsInfoEntity> page = chargedItemsServiceImpl.findAllByParams(pageNum,pageSize,column,order, chargedItemsInfo,chargeCodeOrName);
        System.out.println("page.getPageNum()="+page.getPageNum());
        System.out.println("page.getPageSize()="+page.getPageSize());
        System.out.println("page.getTotal()="+page.getTotal());
        System.out.println("page.getList()="+page.getList());
        return JSONObject.toJSONString(new ResponseMsg(200,"查询成功",page));
    }

    /**
     * 查询所有的“收费项目”
     * @author hejie
     * @return
     */
    @RequestMapping(value="/all",produces = {"application/json;charset=utf-8"})
    public ResponseMsg findAllChargedItems(){
        System.out.println("\nin findAllChargedItems="+ TraceUtil.positionTrace());

        List<ChargedItemsInfoEntity> allChargedItems = chargedItemsServiceImpl.findAllChargedItems();
        return new ResponseMsg(200,"查询成功",allChargedItems);
    }


}
